package com.jiao.service.impl;

import com.jiao.Exception.BizException;
import com.jiao.domain.Result;
import com.jiao.enums.ExceptionCodeEnum;
import com.jiao.service.UploadImgService;
import com.jiao.utils.OssUtils;
import com.jiao.utils.PathUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Service("OssUploadImgService")
public class OssUploadImgServiceImp implements UploadImgService {
    @Autowired
    OssUtils ossUtils;

    @Override
    public Result<String> uploadImg(MultipartFile img) throws IOException {
        //获取原始文件名,判断文件类型
        String originalFilename = img.getOriginalFilename();
        //对原始文件名进行判断
        if(!originalFilename.endsWith(".png")){
            throw new BizException(ExceptionCodeEnum.IMG_TYPE_ERROR);
        }
        //如果判断通过上传文件到OSS
        String filePath = PathUtils.generateFilePath(originalFilename);
        String url = ossUtils.uploadOss(img, filePath);//  2099/2/3/wqeqeqe.png
        return Result.success(url);
    }

}
